import axios from 'axios';
import { API_BASE_URL, API_ENDPOINTS } from './config';

const api = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000,
});

export interface NewsItem {
  id: number;
  title: string;
  summary: string;
  image: string;
}

export interface NewsDetail {
  id: number;
  title: string;
  content: string;
  publishTime: string;
  author: string;
  source: string;
  images: string[];
  tags: string[];
  readCount: number;
}

export const newsService = {
  // 获取新闻列表
  getNewsList: async (categoryId?: number, page: number = 1) => {
    const { data } = await api.get(API_ENDPOINTS.NEWS_LIST, {
      params: { categoryId, page }
    });
    return data;
  },

  // 获取新闻详情
  getNewsDetail: async (id: number) => {
    const { data } = await api.get(`${API_ENDPOINTS.NEWS_DETAIL}/${id}`);
    return data;
  },

  // 获取新闻分类
  getCategories: async () => {
    const { data } = await api.get(API_ENDPOINTS.CATEGORIES);
    return data;
  }
};